package cn.laixueit.cart.controller;

import cn.laixueit.cart.pojo.item.Brand;
import cn.laixueit.cart.pojo.page.PageResult;
import cn.laixueit.cart.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("/page")
    public ResponseEntity<PageResult<Brand>> page(@RequestParam(required = false)String key,
                                                  @RequestParam(required = true)Integer page,
                                                  @RequestParam(required = true)Integer rows,
                                                  @RequestParam(required = true)String sortBy,
                                                  @RequestParam(required = true)Boolean desc){

        PageResult<Brand> pageResult = brandService.findPage(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(pageResult);
    }


    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Brand brand, @RequestParam("cids")List<Long> cids){
        brandService.saveBrandAndCategory(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> cid(@PathVariable(value = "cid")Long cid){
        List<Brand> brandList = brandService.cid(cid);
        return ResponseEntity.ok(brandList);
    }


    @GetMapping("/{id}")
    public ResponseEntity<Brand> getBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(brandService.getById(id));
    }
}
